【Leetcode25】Reverse Nodes in k-Group k个一组翻转链表

[Leetcode25] Reverse Nodes in k-Group k个一组翻转链表


“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表。
* k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序。
* 示例 :
* 给定这个链表:1->2->3->4->5
* 当 k = 2 时,应当返回: 2->1->4->3->5
* 当 k = 3 时,应当返回: 3->2->1->4->5
* 说明 :
* 你的算法只能使用常数的额外空间。
* 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
*/
public class leetcode25 {
// 1 2 3 4 5// 2 1 4 3 5
public ListNode reverseKGroup (ListNode head, int k) {
int length = 0;
ListNode temp = head;
while (temp != null) {
++length;
temp = temp.next;
}
int count = length / k;
ListNode current = head, pre = head, post = head, preTail = null, currentTail = head;
for (int i = 0; i < count; ++i) {
for (int j = 0; j < k; ++j) {
if (j == 0) {
post = current.next;
pre = current;
currentTail = current;
current = post;
} else {
post = current.next;
current.next = pre;
pre = current;
current = post;
}
}
if (preTail == null) {
preTail = currentTail;
} else {
preTail.next = pre;
preTail = currentTail;
}
if (i == 0)
head = pre;
}
if (preTail != null)
preTail.next = current;
return head;
}
}
Thanks!